package com.cjz.demo7;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * observable介绍
 */
public class Test {

    public static void main(String[] args) throws InterruptedException {

        testDemo();

        Thread.sleep(5_000);
    }

    private static void testDemo() {

        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("abc");
                e.onComplete();
            }
        });

        Observer<String> observer = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String value) {
                System.out.println(value);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };

        observable = observable.subscribeOn(Schedulers.newThread());
        observable = observable.observeOn(Schedulers.computation());

        observable.subscribe(observer);
    }
}
